package com.grape.api;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageInfo;
import com.grape.annotation.OperateMethodName;
import com.grape.controller.BaseController;
import com.grape.result.R;
import com.grape.vo.BaseVo;
import com.grape.domain.SysDict;
import com.grape.domain.SysDictItem;
import com.grape.service.ISysDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.logging.log4j.util.Strings;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;


/**
 * 字典类型表 控制器
 * @author  putao
 * @email   2628279194@qq.com
 */
@RestController
@RequestMapping("dicts")
@Api(tags = "字典类型接口")
@AllArgsConstructor
public class SysDictController extends BaseController<SysDict> {

    private final ISysDictService sysDictService;

    @GetMapping
    @ApiOperation(value = "字典类型列表")
    @OperateMethodName(value = "字典类型表列表", url = "system:dicts:listPage")
    public R<?> listPage(BaseVo baseVo) {
        PageInfo<SysDict> pageInfo = sysDictService.pageListByDelFlag(baseVo);
        List<SysDict> sysDictList = pageInfo.getList();
        return CollectionUtil.isNotEmpty(sysDictList) ? selectSuccess(pageInfo, baseVo) : selectNotFound(pageInfo, baseVo);
    }

    @GetMapping("listAll")
    @ApiOperation(value = "字典类型列表")
    @OperateMethodName(value = "字典类型表列表", url = "system:dicts:listAll")
    public R<?> listAll() {
        List<SysDict> sysDictList = sysDictService.list();
        return CollectionUtil.isNotEmpty(sysDictList) ? selectSuccess(sysDictList, null) : selectNotFound(sysDictList, null);
    }

    @PutMapping
    @ApiOperation(value = "字典类型修改")
    @OperateMethodName(value = "字典类型表修改", url = "system:dicts:update")
    public R<?> update(@Validated @RequestBody SysDict sysDict) {
        if (!Strings.isNotBlank(sysDict.getId())){
            return updateNotFound(sysDict, null);
        }
        sysDictService.updateData(sysDict);
        return updateSuccess(null, null);
    }


    @PostMapping
    @ApiOperation(value = "添加字典类型")
    @OperateMethodName(value = "添加字典类型表", url = "system:dicts:insert")
    public R<?> insert(@Validated @RequestBody SysDict sysDict) {
        return insertSuccess(sysDictService.insertData(sysDict), null);
    }

    @DeleteMapping
    @ApiOperation(value = "删除字典类型，可批量多个id逗号拼接")
    @OperateMethodName(value = "字典类型表删除,批量", url = "system:dicts:delete")
    public R<?> delete(@NotBlank(message = "删除时ids不能为空")  @RequestParam String ids) {
        int number = sysDictService.deleteData(ids);
        if (number < 0) {
            return deleteNotFound(ids, number);
        }
        return deleteSuccess(ids, null);
    }

    @GetMapping("detail")
    @ApiOperation(value = "字典类型详情")
    @OperateMethodName(value = "字典类型表详情", url = "system:dicts:detail")
    public R<?> detail(@NotBlank(message = "详情时id不能为空") String id) {
        SysDict sysDict = sysDictService.detail(id);
        if (sysDict != null) {
            return selectSuccess(sysDict, id);
        }
        return selectNotFound(id, null);
    }

    @GetMapping("listByCode")
    @ApiOperation(value = "根据字典code查询字典项")
    @OperateMethodName(value = "根据字典code查询字典项", url = "system:dicts:listByCode")
    public R<?> listByCode(@NotBlank(message = "字典编码不能为空") String code) {
        List<SysDictItem> dictItemList = sysDictService.listByCode(code);
        if (CollectionUtil.isEmpty(dictItemList)) {
            return selectNotFound(code, null);
        }
        return selectSuccess(dictItemList, null);
    }
}

